﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Shove.Web.UI;

public partial class UserControls_GoodList : UCBase
{
    public UserControls_GoodList()
    {
        Name = "商品列表";

        swpas = new ShoveWebPartAttribute[22];

        swpas[0] = new ShoveWebPartAttribute_Int("控件宽度", "1,1004", 759, "控件宽度");
        swpas[1] = new ShoveWebPartAttribute_Int("控件高度", "1,1004", 300, "控件高度");

        swpas[2] = new ShoveWebPartAttribute_Color("控件边框颜色", "#E7C890", "控件边框颜色");

        swpas[3] = new ShoveWebPartAttribute_Image("导航背景图片1", "", "images/UserControls/title_bg_hui.jpg", "导航背景图片1");
        swpas[4] = new ShoveWebPartAttribute_Image("导航背景图片2", "", "images/UserControls/title_bg_hui.jpg", "导航背景图片2");
        swpas[5] = new ShoveWebPartAttribute_Text("导航字体样式", "", "black12", "导航字体样式");

        swpas[6] = new ShoveWebPartAttribute_Text("标题CSS样式", "", "black12", "标题CSS样式");
        swpas[7] = new ShoveWebPartAttribute_Text("产品图片样式", "", "photo", "产品图片样式");
        swpas[8] = new ShoveWebPartAttribute_Text("产品名称样式", "", "td14", "产品名称样式");
        swpas[9] = new ShoveWebPartAttribute_Text("商品名CSS样式", "", "pro_name", "商品名CSS样式");
        swpas[10] = new ShoveWebPartAttribute_Text("市场价CSS样式", "", "market_price", "市场价CSS样式");
        swpas[11] = new ShoveWebPartAttribute_Text("会员价CSS样式", "", "blue12", "会员价CSS样式");
        swpas[12] = new ShoveWebPartAttribute_Text("立即节省CSS样式", "", "black12", "立即节省CSS样式");
        swpas[13] = new ShoveWebPartAttribute_Text("产品介绍样式", "", "td12", "产品介绍样式");

        swpas[14] = new ShoveWebPartAttribute_Int("产品图片宽度", "1,300", 120, "产品图片宽度");
        swpas[15] = new ShoveWebPartAttribute_Int("产品图片高度", "1,300", 120, "产品图片高度");
        swpas[16] = new ShoveWebPartAttribute_Int("名称行高", "1,300", 30, "名称行高");
        swpas[17] = new ShoveWebPartAttribute_Int("每页几条数据", "1,50", 5, "每页几条数据");

        swpas[18] = new ShoveWebPartAttribute_Image("马上购买图片", "", "images/UserControls/button_car_5.jpg", "马上购买图片");
        swpas[19] = new ShoveWebPartAttribute_Image("查看详细图片", "", "images/UserControls/button_car_6.jpg", "查看详细图片");
        swpas[20] = new ShoveWebPartAttribute_Image("加入收藏图片", "", "images/UserControls/button_car_7.jpg", "加入收藏图片");
        swpas[21] = new ShoveWebPartAttribute_Image("分割线", "", "images/UserControls/line_bg2.gif", "分割线");
    }

    private void InitlayOut()
    {
        //tabctr.Width = swpAttributes[0];
        //tabctr.Height = swpAttributes[1];

        //tdsign.Height = swpAttributes[1];
        //tdsign.Width = swpAttributes[2];
        //tdsign.Attributes.Add("class", swpAttributes[6]);
        //tdsign.Style.Add("background-image", "url('" + swpAttributes[4] + "')");
        //tdspace.Style.Add("background-image", "url('" + swpAttributes[5] + "')");
        //tdspace.Width = (Shove._Convert.StrToInt(swpAttributes[0], 759) - Shove._Convert.StrToInt(swpAttributes[2], 409)).ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["CurrentPage"] = null;
        }

        Page1.PageSize = Shove._Convert.StrToInt(swpAttributes[18], 5);

        InitlayOut();
        FillData();
    }

    private void FillData()
    {
        string Page = Shove._Web.Utility.GetRequest("Page");

        if (Page == "")
        {
            LoadGood();
        }

        if (Page == "New")              // 新品上架
        {
            LoadNewGood();
        }

        if (Page == "Commend")          // 今日推荐
        {
            LoadCommendGood();
        }

        if (Page == "Favorable")        // 特价(优惠)商品
        {
            LoadFavorableGood();
        }

        if (Page == "Hot")              // 热卖产品
        {
            LoadHotGood();
        }

        if (Page == "Brand")            // 根据品牌搜索
        {
            LoadBrandGood();
        }

        if (Page == "Type")            // 根据类型搜索
        {
            LoadTypeGood();
        }

        if (Page == "Search")           // 根据关键字搜索
        {
            SearchGood();
        }

        if (Page == "HotSpot")          // 根据热点搜索
        {
            LoadHotSpotGood();
        }
    }

    /// <summary>
    /// 加载该店铺所有产品
    /// </summary>
    private void LoadGood()
    {
        long ShopID = _Site.ID;

        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Site_GoodShopStock";
        this.Page1.Condition = "where isOnSale = 1 and ShopID=1";  //+ _Site.ID;
    }

    /// <summary>
    /// 分类，绑定数据
    /// </summary>
    private void LoadTypeGood()
    {
        long ShopID = _Site.ID;

        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Site_GoodShopStock";
        this.Page1.Condition = "where isOnSale = 1 and ShopID=1 and GoodTypeID like '" + Shove._Web.Utility.GetRequest("GoodType") + "%'";
    }

    /// <summary>
    /// 品牌，绑定数据
    /// </summary>
    private void LoadBrandGood()
    {
        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Site_GoodShopStock";
        this.Page1.Condition = "where isOnSale = 1 and ShopID=1 and BrandID=" + Shove._Web.Utility.GetRequest("ID");
    }

    /// <summary>
    /// 最新商品，绑定数据
    /// </summary>
    private void LoadNewGood()
    {
        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Site_GoodShopStock";
        this.Page1.Condition = "where isOnSale = 1 and ShopID=1 and isNew=1";
    }

    /// <summary>
    /// 推荐商品，绑定数据
    /// </summary>
    private void LoadCommendGood()
    {
        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Site_GoodShopStock";
        this.Page1.Condition = "where isOnSale = 1 and ShopID=1 and IsCommend=1";
    }

    /// <summary>
    /// 特价产品，绑定数据
    /// </summary>
    private void LoadFavorableGood()
    {
        long ShopID = _Site.ID;
        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Site_GoodShopStock";
        this.Page1.Condition = "where isOnSale = 1 and ShopID=1 and IsFavorable=1";
    }

    /// <summary>
    /// 热卖商品，绑定数据
    /// </summary>
    private void LoadHotGood()
    {
        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Site_GoodShopStock";
        this.Page1.Condition = "where isOnSale = 1 and ShopID=1 and isHot=1";
    }

    /// <summary>
    /// 今日热点，绑定数据
    /// </summary>
    private void LoadHotSpotGood()
    {
        string KeyWord = Shove._Web.Utility.GetRequest("KeyWord");
        string Query = "where ShopID=" + _Site.ID;
        Query += " and (GoodName_CN Like ,%" + KeyWord + "%,";
        Query += " or TypeName Like ,%" + KeyWord + "%,";
        Query += " or GoodNorm Like ,%" + KeyWord + "%,";
        Query += " or GoodIntroduce Like ,%" + KeyWord + "%,";
        Query += " or GoodIntroduce_Detail Like ,%" + KeyWord + "%,";
        Query += ")";

        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Good_ShopStock";
        this.Page1.Condition = Query;
    }

    /// <summary>
    /// 关键字搜索产品，绑定数据，查询条件中有单引号用逗号代替
    /// </summary>
    private void SearchGood()
    {
        string GoodType = Shove._Web.Utility.GetRequest("GoodType");
        string KeyWord = Shove._Web.Utility.GetRequest("KeyWord");

        string Query = "where ShopID=1";  //+_Site.ID;

        if (GoodType != "")
        {
            Query += " and GoodTypeID like '" + GoodType + "%'";
        }
        if (KeyWord != "")
        {
            Query += " and (GoodName_CN Like ,%" + KeyWord + "%,";
            //Query += " or TypeName Like ,%" + KeyWord + "%,";
            Query += " or GoodNorm Like ,%" + KeyWord + "%,";
            //Query += " or GoodIntroduce Like ,%" + KeyWord + "%,";
            //Query += " or GoodIntroduce_Detail Like ,%" + KeyWord + "%,";
            Query += ")";
        }

        this.Page1.FindType = 0;
        this.Page1.Field1 = "";
        this.Page1.Field2 = "";
        this.Page1.Like1 = "";
        this.Page1.Like2 = "";

        this.Page1.DataListID = dlGoods;
        this.Page1.TableName = "V_Site_GoodShopStock";
        this.Page1.Condition = Query;
    }

    /// <summary>
    /// 加入收藏夹
    /// </summary>
    protected void dlGoods_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            if (_Customer == null)
            {
                Response.Redirect("Default.aspx?PN=Login");
            }
            else
            {
                long ShopID = _Site.ID;
                DataTable dt = new DAL.Tables.tbl_Site_FatoviteGood().Open("", "ShopID=" + ShopID + " and CustomerID=" + _Customer.ID + "", "");

                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr["GoodID"].ToString() == ((HiddenField)e.Item.FindControl("hID")).Value.Split(',')[0])
                        {
                            Shove._Web.JavaScript.Alert(this.Page, "此商品已在你的个人收藏中！");
                            return;
                        }
                    }
                }

                DAL.Tables.tbl_Site_FatoviteGood f = new DAL.Tables.tbl_Site_FatoviteGood();
                f.CustomerID.Value = _Customer.ID;
                f.ShopID.Value = ShopID;
                f.GoodID.Value = Shove._Convert.StrToLong(((HiddenField)e.Item.FindControl("hID")).Value.Split(',')[0], 0);
                f.Insert();

                Shove._Web.JavaScript.Alert(this.Page, "收藏成功！");
            }
        }
    }

    /// <summary>
    /// 加入购物车
    /// </summary>
    protected void dlGoods_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        long ShopID = _Site.ID;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image img = (Image)e.Item.FindControl("imgGoodPic");
            img.ImageUrl = "~/UpFile/Goods/" + img.ImageUrl;
            img.Width = new Unit(swpAttributes[14]);
            img.Height = new Unit(swpAttributes[15]);
            img.CssClass = swpAttributes[8];

            HtmlTableCell tdname = (HtmlTableCell)e.Item.FindControl("tdname");
            tdname.Height = swpAttributes[17];
            tdname.Attributes.Add("class", swpAttributes[9]);

            HtmlTableCell tdcontent = (HtmlTableCell)e.Item.FindControl("tdcontent");
            tdcontent.Attributes.Add("class", swpAttributes[13]);

            HiddenField hID = (HiddenField)e.Item.FindControl("hID");
            ImageButton ibBuy = (ImageButton)e.Item.FindControl("ibBuy");
            ibBuy.ImageUrl = "~/" + swpAttributes[18];

            HtmlGenericControl spanmark = (HtmlGenericControl)e.Item.FindControl("spanmark");
            spanmark.Attributes.Add("class", swpAttributes[10]);

            HtmlGenericControl spanvip = (HtmlGenericControl)e.Item.FindControl("spanvip");
            spanvip.Attributes.Add("class", swpAttributes[11]);

            HtmlGenericControl spanCost = (HtmlGenericControl)e.Item.FindControl("spanCost");
            spanCost.Attributes.Add("class", swpAttributes[13]);

            ImageButton ibFav = (ImageButton)e.Item.FindControl("ibFav");
            ibFav.ImageUrl = "~/" + swpAttributes[20];

            HtmlTableCell tdline = (HtmlTableCell)e.Item.FindControl("tdline");
            tdline.Style.Add("background-image", "url('" + swpAttributes[21] + "')");
            HtmlImage imglook = (HtmlImage)e.Item.FindControl("imglook");
            imglook.Src = "~/" + swpAttributes[19];

            string GoodNorm = "";
            string GoodColor = "";

            string GoodID = new DAL.Tables.tbl_Shop_ShopStock().Open("distinct GoodID", "ID=" + hID.Value, "").Rows[0]["GoodID"].ToString();
            //string GoodNorm = new DAL.Tables.tbl_ShopStock().Open("distinct GoodNorm", "GoodID=" + hID.Value + " and ShopID=" + _Site.ID + "", "").Rows[0]["GoodNorm"].ToString();
            //string GoodColor = new DAL.Tables.tbl_ShopStock().Open("distinct GoodColor", "GoodID=" + hID.Value + " and ShopID=" + _Site.ID + "", "").Rows[0]["GoodColor"].ToString();

            ibBuy.Attributes.Add("onclick", "addGood('" + GoodID + "','" + GoodNorm + "','" + GoodColor + "')");
        }
    }

}
